
package com.ukao.steward.pesenter.valetRunners;

import com.ukao.steward.base.BasePresenter;
import com.ukao.steward.bean.SearchUserBean;
import com.ukao.steward.bean.StringBean;
import com.ukao.steward.bean.UserInfoCard;
import com.ukao.steward.consts.Constant;
import com.ukao.steward.consts.SaveParams;
import com.ukao.steward.retrofit.ApiCallback;
import com.ukao.steward.util.L;
import com.ukao.steward.util.T;
import com.ukao.steward.view.valetRunners.ClientView;

import java.util.HashMap;

/**
 * Created by ukaodev on 2017/6/28.
 */

public class ClientPesenter extends BasePresenter<ClientView> {


    public ClientPesenter(ClientView view) {
        attachView(view);
    }






    /**
     绑卡
     */
    public void cardBinding( String  userId,String  rfidId) {
        mvpView.showLoading();
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("userId",userId);
        mHashMap.put("rfidId",rfidId);
        mHashMap.put("accessToken", SaveParams.getToken());
        addSubscription(apiStores.cardBinding(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {

                        if (model.getHttpCode() == 200) {
                              mvpView.cardBindingSucceed(model.getData());
                        }else{

                            mvpView.showError(model.getMsg());
                        }

                    }


                    @Override
                    public void onFinish() {

                        mvpView.hideLoading();

                    }
                });

    }


    /**
     * 手机号搜索
     */
    public void queryKeyword(final  String cardNumber,String rfidId) {
        mvpView.showLoading();
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("rfidNo",cardNumber);
        mHashMap.put("rfidSecret","");
        mHashMap.put("accessToken", SaveParams.getToken());
        addSubscription(apiStores.userInfoCard(Constant.createParameter(mHashMap)),
                new ApiCallback<UserInfoCard>() {
                    @Override
                    public void onSuccess(UserInfoCard model) {

                        if (model.getHttpCode() == 200) {
                            UserInfoCard muserInfoCard=model.getData();
                            if (muserInfoCard.getIsBinding()){
                                mvpView.queryKeyword(muserInfoCard);

                            }

                        } else {


                            mvpView.showError(model.getMsg());
                        }

                    }


                    @Override
                    public void onFinish() {

                        mvpView.hideLoading();

                    }
                });


    }


    /**
     * 手机号搜索
     *
     * @param keyword
     */
    public void queryKeyword(final  String keyword) {
        mvpView.showLoading();
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("keyword",keyword);
        mHashMap.put("accessToken", SaveParams.getToken());
        L.i("mHashMap="+mHashMap);
        addSubscription(apiStores.queryKeywordUse(Constant.createParameter(mHashMap)),
                new ApiCallback<SearchUserBean>() {
                    @Override
                    public void onSuccess( SearchUserBean model) {

                        if (model.getHttpCode() == 200) {

                            mvpView.queryPhoneSucced(model.getData());


                        } else {

                            T.show(model.getMsg());

                        }

                    }


                    @Override
                    public void onFinish() {

                       mvpView.hideLoading();

                    }
                });


    }
}
